package com.atguigu.tingshu.user.api;

import com.atguigu.tingshu.common.login.TsLogin;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.model.user.UserInfo;
import com.atguigu.tingshu.user.service.UserInfoService;
import com.atguigu.tingshu.vo.user.UserInfoVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "用户管理接口")
@RestController
@RequestMapping("api/user/userInfo")
@SuppressWarnings({"unchecked", "rawtypes"})
public class UserInfoApiController {

	@Autowired
	private UserInfoService userInfoService;
	@TsLogin
	@Operation(summary = "更新用户信息")
	@PostMapping("updateUser")
	public Result updateUser(@RequestBody UserInfoVo userInfoVo){
		//  获取到用户Id
		Long userId = AuthContextHolder.getUserId()==null?AuthContextHolder.getUserId():1L;
		UserInfo userInfo = new UserInfo();
		userInfo.setId(userId);
		userInfo.setNickname(userInfoVo.getNickname());
		userInfo.setAvatarUrl(userInfoVo.getAvatarUrl());
		//  执行更新方法
		userInfoService.updateById(userInfo);
		return Result.ok();
	}
	@Operation(summary = "根据用户id获取用户信息")
	@GetMapping("getUserInfoVo/{userId}")
	public Result<UserInfoVo> getUserInfoVo(@PathVariable Long userId) {
		// 获取用户信息
		UserInfoVo userInfoVo = userInfoService.getUserInfoVoByUserId(userId);
		return Result.ok(userInfoVo);
	}
	@TsLogin(required = false)
	@Operation(summary = "判断用户是否购买声音列表")
	@PostMapping("/userIsPaidTrack/{albumId}")
	Result<List<Long>> userIsPaidTrack(@PathVariable Long albumId, @RequestBody List<Long> trackIdList){
		Long userId = AuthContextHolder.getUserId();
		List<Long> list= userInfoService.userIsPaidTrack(albumId,userId, trackIdList);
		return Result.ok(list);
	}
	@TsLogin
	@Operation(summary = "判断用户是否购买过专辑")
	@GetMapping("isPaidAlbum/{albumId}")
	public Result<Boolean> isPaidAlbum(@PathVariable Long albumId) {
		// 获取到用户Id
		Long userId = AuthContextHolder.getUserId();
		// 调用服务层方法
		Boolean flag = userInfoService.isPaidAlbum(userId, albumId);
		return Result.ok(flag);
	}
}

